<?php
/**
 * 
 * UI Template Plugin
 * 
 * The template plugin loads the template skin
 * which is passed as the 'digitalus_template' and 'digitalus_skin' request parameters
 * this parameters are set before the controller is dispatched, so you are able 
 * to override them as necessary
 * 
 * The template plugin sets and handles the site template
 *
 * @package    	Digitalus\Ui
 * @copyright  	Copyright (c) 2009,  Digitalus Media USA (http://digitalusmedia.net)
 * @license    	New BSD License, library/Dig/License.txt
 * @version 	2.0
 * 
 */
class DigUi_Plugin_Template extends Zend_Controller_Plugin_Abstract 
{
    protected $_isAjaxRequest = false;

    /**
     * sets the template to the default template if it is not already set
     * the default template is defined in the UI module config file
     *
     * @param Zend_Controller_Request_Abstract $request
     */
    public function postDispatch(Zend_Controller_Request_Abstract $request)
    {

        //Zend_Debug::dump($request->getParams());die();
        if( $request->isXmlHttpRequest() ) {
            DigUi_Service_Template::useAjaxTemplate();
        }
        $layout = Zend_Layout::getMvcInstance();
        $layoutDisabled = $request->getParam('disable_layout', false);
        if ($layoutDisabled || !$layout->isEnabled()) {
            $layout->disableLayout();
        } else {
            // set the layout
            // if an item is set then use that skin
            if(isset($layout->getView()->item) && 
                    ! DigUi_Service_Template::isAdminTemplate() &&
                    ! DigUi_Service_Template::isAjaxTemplate()) {
                $view = $layout->getView();
                DigUi_Service_Template::setItemSkin($view->item);
                // set filters
                // TODO move this to config
                $view->addFilterPath(APPLICATION_PATH . '/modules/digCms/views/filters', 'DigCms_View_Filter');
                $view->setFilter('Include');
                $view->setFilter('Children');
                $view->setFilter('Partial');
            }
            $layout->setLayoutPath( DigUi_Service_Template::getPath(null, false) . '/layouts');
            $layout->setLayout( DigUi_Service_Template::currentLayout() );
            Dig_Service_Firebug::profile('loading layout - ' . DigUi_Service_Template::currentLayout());
        }
    }
}
?>